package com.sinosoft.ci.patch.yqscm;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

import com.sinosoft.ci.patch.SrcEntryToPatchEntryConvertor;
import com.sinosoft.ci.patch.svn.GuoLianLisSrcEntryToPatchEntryConvertor;

public class YQScmFileExtractExcel {
	private Workbook excel;
	private File appRootDir;
	
	private static final String JAVA_SHEET_NAME = "JAVA";
	private static final String UI_SHEET_NAME = "UI";
	
	private static final int JAVA_SHEET_START_INDEX = 6;
	private static final int UI_SHEET_START_INDEX = 4;
	
	private SrcEntryToPatchEntryConvertor entryConvertor;
	
	public YQScmFileExtractExcel(File excelFile,File appRootDir){
		if(null == excelFile || null == appRootDir){
			throw new IllegalArgumentException("文件不能为 null！");
		}
		
		try{
			excel = WorkbookFactory.create(excelFile);
		}catch(Exception ex){
			throw new IllegalArgumentException(String.format("excel 文件 %s 解析失败！", excelFile.getAbsolutePath()));
		}
		
		if(!appRootDir.exists() || !appRootDir.isDirectory()){
			throw new IllegalArgumentException(String.format("目录 %s 不是一个合法的目录！！", appRootDir.getAbsolutePath()));
		}
		this.appRootDir = appRootDir;
		entryConvertor = new GuoLianLisSrcEntryToPatchEntryConvertor(appRootDir);
	}
	
	public YQScmFileExtractExcel(Workbook excel,File appRootDir){
		if(null == excel || null == appRootDir){
			throw new IllegalArgumentException("文件不能为 null！");
		}
		this.excel = excel;
		if(!appRootDir.exists() || !appRootDir.isDirectory()){
			throw new IllegalArgumentException(String.format("目录 %s 不是一个合法的目录！！", appRootDir.getAbsolutePath()));
		}
		this.appRootDir = appRootDir;
		entryConvertor = new GuoLianLisSrcEntryToPatchEntryConvertor(appRootDir);
	}
	
	/**
	 * 获取全部源码条目
	 * 
	 * @return
	 */
	public String[] getPatchEntry(){
		Sheet javaSheet = getJavaSheet();
		Row row = null;
		
		List<String> entrys = new LinkedList<String>();
		
		// 处理 java Sheet 页
		int index = JAVA_SHEET_START_INDEX;
		while((row = javaSheet.getRow(index)) != null){
			YQScmFileExtractJavaRow eRow = new YQScmFileExtractJavaRow(row);
			if(eRow.isValid()){
				addPatchEntry(entrys, eRow);
			}
			index++;
		}
		
		// 处理 UI Sheet 页
		Sheet uiSheet = getUISheet();
		index = UI_SHEET_START_INDEX;
		while((row = uiSheet.getRow(index)) != null){
			YQScmFileExtractUIRow eRow = new YQScmFileExtractUIRow(row);
			if(eRow.isValid()){
				addPatchEntry(entrys, eRow);
			}
			index++;
		}
		
		return entrys.toArray(new String[entrys.size()]);
	}
	
	/**
	 * 解析
	 * @param entrys
	 * @param row
	 */
	private void addPatchEntry(List<String> entrys,final AbstractYQScmFileExtractRow row){
		String[] patchEntrys = entryConvertor.convert(row.getSrcEntryPath());
		for(String e : patchEntrys){
			if(!entrys.contains(e)){
				entrys.add(e);
			}
		}
	}
	
	private Sheet getJavaSheet(){
		return excel.getSheet(JAVA_SHEET_NAME);
	}
	
	private Sheet getUISheet(){
		return excel.getSheet(UI_SHEET_NAME);
	}
}
